package com.nowcoder.community.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;

/**
 * @author szq
 * @create 2023-02-10 21:35
 * @description
 */
//@ConfigurationProperties(prefix = "elasticsearch")
//@Configuration
@Data
public class ElasticsearchConfig {

    private String host;
    private Integer port;


    /**
     * 初始化es-client.
     *
     * @return RestHighLevelClient
     */
    @Bean
    public ElasticsearchClient elasticsearchClient() {
        RestClient restClient = RestClient.builder(new HttpHost(host, port)).build();
        // 使用Jackson映射器创建传输
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        // 创建API客户端
        return new ElasticsearchClient(transport);
    }
}
